﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
Imports System.Security.Cryptography
Imports System.Data.SqlClient
Imports System.Data

NotInheritable Class GlobalClass
    Private Sub New()
    End Sub
    Public Shared _UserName As String = ""
    Public Shared _Password As String = ""
    Public Shared _FullName As String = ""
    Public Shared _PermissionID As Int32 = -1
    Public Shared _UserID As Int32 = -1
    Public Shared _Today As DateTime = DateTime.Now

    Public Shared _ConStr As String = Settings.[Default].ConnectionStr

    Public Shared Function GetCurrentDateTime() As DateTime
        Dim dt As DateTime = DateTime.MinValue
        Dim objSqlDatabase As New SqlDatabase(_ConStr)
        Try
            Dim commandText As String = "SELECT GETDATE() AS Today "

            dt = CType(objSqlDatabase.ExecuteScalar(System.Data.CommandType.Text, commandText), DateTime)
        Catch generatedExceptionName As SqlException
        End Try
        Return dt
    End Function

    Public Shared Function GetMd5Hash(ByVal input As String) As String
        ' Create a new instance of the MD5CryptoServiceProvider object.
        Dim md5Hasher As MD5 = MD5.Create()

        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.[Default].GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data 
        ' and format each one as a hexadecimal string.
        For i As Integer = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("X2"))
        Next

        ' Return the hexadecimal string.
        Return sBuilder.ToString()
    End Function

    Public Shared Function GetRestaurantInfo() As DataTable
        Dim objSqlDatabase As New SqlDatabase(_ConStr)
        Try
            Dim commandText As String = "SELECT * FROM Settings"
            Dim ds As DataSet = objSqlDatabase.ExecuteDataSet(System.Data.CommandType.Text, commandText)
            If ds IsNot Nothing Then
                Return ds.Tables(0)
            Else
                Return Nothing
            End If
        Catch generatedExceptionName As SqlException
            Return Nothing
        End Try
    End Function
End Class

